package com.ctsi.flowable.engine.service;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.flowable.engine.entity.LinkRouteInfo;
import com.ctsi.framework.security.util.SecurityUtil;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEvent;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.delegate.event.FlowableActivityEvent;
import org.flowable.engine.delegate.event.FlowableCancelledEvent;
import org.flowable.engine.delegate.event.FlowableSignalEvent;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author: zhangjw
 * @Date: 2018/10/24 10:32
 * @Description: 流程全局监听器
 */
//@Component("flowGlobalEventListener")
public class FlowGlobalEventListener extends AbstractFlowableEngineEventListener {

    private static final Logger logger = LoggerFactory.getLogger(FlowGlobalEventListener.class);

    @Autowired
    private RuntimeService runtimeService;

    @Override
    protected void taskCreated(FlowableEngineEntityEvent event) {
        TaskEntity task = (TaskEntity) event.getEntity();
        logger.debug("create task is "+task.getName()+" key is:"+task.getTaskDefinitionKey());
        // 使用任务ID，获取实例ID
        String processInstanceId = task.getProcessInstanceId();
        // 使用流程实例，查询
        ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        // 使用流程实例对象获取BusinessKey
        String business_key = pi.getBusinessKey();
        // 处理执行任务的标题
        String processSubject=(String)task.getVariable("processSubject");
        if (UtilValidate.isEmpty(task.getDescription())) {
            task.setDescription(processSubject);
        }
        if (UtilValidate.isEmpty(task.getTenantId())) {
            task.setTenantId(SecurityUtil.getUser().getTenementId());
        }
        // 记录环节信息
        LinkRouteInfo routeInfo = new LinkRouteInfo();
        routeInfo.setProcessDefId(task.getProcessDefinitionId());
        routeInfo.setProcessInstId(task.getProcessInstanceId());

        routeInfo.setActDefId(task.getTaskDefinitionId());
        routeInfo.setActInstId(task.getId());
        routeInfo.setCreateTime(task.getCreateTime().getTime());





    }

    @Override
    protected void taskAssigned(FlowableEngineEntityEvent event) {
        super.taskAssigned(event);
    }

    @Override
    protected void taskCompleted(FlowableEngineEntityEvent event) {
        super.taskCompleted(event);
    }

    @Override
    protected void processCompleted(FlowableEngineEntityEvent event) {
        super.processCompleted(event);
    }

    @Override
    protected void processCancelled(FlowableCancelledEvent event) {
        super.processCancelled(event);
    }

    @Override
    protected void custom(FlowableEngineEvent event) {
        super.custom(event);
    }

    @Override
    protected void activityStarted(FlowableActivityEvent event) {
        super.activityStarted(event);
    }

    @Override
    protected void activityCompleted(FlowableActivityEvent event) {
        super.activityCompleted(event);
    }

    @Override
    protected void activitySignaled(FlowableSignalEvent event) {
        super.activitySignaled(event);
    }
}
